package service;

import java.util.ArrayList;
import java.util.List;
import model.Product;

public class ProductService {
    private List<Product> products = new ArrayList();

    public ProductService() {
    }

    public void addProduct(Product product) {
        this.products.add(product);
    }

    public Product getProduct(int id) {
        return (Product)this.products.stream().filter((p) -> {
            return p.getId() == id;
        }).findFirst().orElse((Product) null);
    }

    public void updateProduct(Product product) {
        Product existingProduct = this.getProduct(product.getId());
        if (existingProduct != null) {
            existingProduct.setName(product.getName());
            existingProduct.setPrice(product.getPrice());
            existingProduct.setQuantity(product.getQuantity());
        }

    }

    public void deleteProduct(int id) {
        this.products.removeIf((p) -> {
            return p.getId() == id;
        });
    }

    public List<Product> listProducts() {
        return new ArrayList(this.products);
    }
}
